package com.sinven.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.sinven.pojo.Article;
import com.sinven.pojo.User;
import com.sinven.service.ArticleService;
import com.sinven.util.PageBean;

/**
 * 我的文章
 * 
 * @author 陈顺
 * @since 2019/06/04
 */
@Controller
public class AboutController {
	@Autowired
	private ArticleService articleService;

	/** 显示博客自己文章数据 */
	@RequestMapping({ "about" })
	public String selectArticle(HttpSession session, Model model,
			@RequestParam(value = "pageNum", defaultValue = "1", required = true) int pageNum,
			@RequestParam(value = "pageSize", defaultValue = "5", required = true) int pageSize) {
		User user = (User) session.getAttribute("user");
		try {
			PageBean<Article> list = articleService.SelectArticleById(user.getU_id(), pageNum, pageSize);
			System.out.println(list);
			List<Article> notReleaselist = articleService.SelectNotRelease(user.getU_id());

			List<Article> deletelist = articleService.SelectPersonalDeleted(user.getU_id());

			model.addAttribute("ArticleNotRelease", notReleaselist);
			model.addAttribute("ArticleContent", list);
			model.addAttribute("ArticleDelete", deletelist);
			return "pages/about";

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "pages/about";
	}

	/** 根据文章id删除一条文章数据 */
	@RequestMapping("deleteItemById")
	public String deleteItemById(@Param("a_id") int a_id) { // 接收页面的请求id
		try {
			boolean res = articleService.DeletedSelfArticle(a_id);
			if (res) {
				System.out.println("删除成功！");
				return "forward:about";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("删除失败！");
		return null; // 
	}
	
	
	@PostMapping("deleteItemId")
	@ResponseBody
	public boolean deleteItemId(@Param("a_id") int a_id) { // 接收页面的请求id
		try {
			boolean res = articleService.DeletedSelfArticle(a_id);
			if (res) {
				System.out.println("删除成功！");
				return res;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("删除失败！");
		return false; // 
	}
	
	
	/** 根据文章id还原一条文章数据 */
	@RequestMapping("BatchReleaseId")
	@ResponseBody
	public String BatchReleaseId(@Param("a_id") int a_id) { // 接收页面的请求id
		try {
			boolean res = articleService.ReductionArticle(a_id);
			if (res) {
				System.out.println("还原成功！");
				return "forward:about";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("还原失败！");
		return null; // 重定向到展示数据页面
	}
	/** 根据文章id还原一条文章数据 */
	@RequestMapping("reductionItemId")
	@ResponseBody
	public String reductionItemId(@Param("a_id") int a_id) { // 接收页面的请求id
		try {
			boolean res = articleService.ReductionArticle(a_id);
			if (res) {
				System.out.println("还原成功！");
				return "forward:about";//转发重新加载about页面
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("还原失败！");
		return null; // 重定向到展示数据页面
	}
	
	@RequestMapping("reduction")
	public String reduction(@Param("a_id") int a_id) { // 接收页面的请求id
		try {
			boolean res = articleService.ReductionArticle(a_id);
			if (res) {
				System.out.println("还原成功！");
				return "forward:about";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("还原失败！");
		return null; // 重定向到展示数据页面
	}
	
	

	@RequestMapping("outLogin")
	public String tologout(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			SessionStatus sessionStatus) {
		User user = (User) session.getAttribute("user");
		session.removeAttribute("user");
		sessionStatus.setComplete();//提交session状态
		session.invalidate();// 清除session 使其无效
		//ModelAndView modelAndView = new ModelAndView("user/Login");// 转到/WEB-INF/jsp/user/Login.jsp
		//session.invalidate(); 
		
		return "../index";//  退出登录 返回登录页面
	}




}

